package cn.edu.njnu.opengmpback.Modeling.controller;


import cn.edu.njnu.opengmpback.Modeling.entity.dto.*;
import cn.edu.njnu.opengmpback.Modeling.service.LogicModelService;
import cn.edu.njnu.opengmpback.common.utils.JsonResult;
import cn.edu.njnu.opengmpback.common.utils.ResultUtils;
import cn.edu.njnu.opengmpback.user.entity.UserVo;
import cn.edu.njnu.opengmpback.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


@RestController
@RequestMapping(value = "/logicModel")
public class LogicModelController {

    @Autowired
    LogicModelService logicModelService;

    @Autowired
    UserService userService;

    @PostMapping("/create")
    @ApiOperation(value = "创建逻辑模型")
    public JsonResult createLogicModel(@RequestPart("info")CreateLogicModelDTO createLogicModelDTO, @RequestPart("imgFile")MultipartFile imgFile, HttpServletRequest request) {
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return logicModelService.createLogicModel(createLogicModelDTO,imgFile,userId);
    }

    @GetMapping("/open/{logicModelId}")
    @ApiOperation(value="打开逻辑模型")
    public JsonResult OpenLogicModel(@PathVariable String logicModelId) {
        return logicModelService.openLogicModel(logicModelId);
    }

    @PostMapping("/getLogicModelList")
    @ApiOperation(value = "查询逻辑建模列表_基于项目")
    public JsonResult getLogicModelList(@RequestBody LogicModelPageDTO logicModelPageDTO) {
        return logicModelService.searchLogicModelList(logicModelPageDTO);
    }

    @PostMapping("/getAllLogicModelList")
    @ApiOperation(value = "查询逻辑建模列表")
    public JsonResult getAllLogicModelList(@RequestBody LogicModelPageDTO logicModelPageDTO) {
        return logicModelService.searchAllLogicModelList(logicModelPageDTO);
    }

    @PostMapping("/update/{logicModelId}")
    @ApiOperation(value = "更新概念建模")
    public JsonResult updateLogicModel(@PathVariable(value = "logicModelId") String logicModelId, @RequestPart("info") CreateLogicModelDTO createLogicModelDTO,
                                       @RequestPart("imgFile") MultipartFile imgFile, HttpServletRequest request) throws IOException {
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        return logicModelService.updateLogicModel(logicModelId,createLogicModelDTO,imgFile);
    }

    @GetMapping("/delete/{logicModelId}")
    @ApiOperation(value = "删除逻辑建模")
    public JsonResult deleteLogicModel(@PathVariable(value = "logicModelId") String logicModelId, HttpServletRequest request) {
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        return logicModelService.deleteLogicModel(logicModelId);
    }

    @PostMapping("/createMaterial")
    @ApiOperation(value = "创建逻辑建模素材")
    public JsonResult createMaterual(@RequestPart("info") CreateLogicMaterialDTO createLogicMaterialDTO,
            @RequestPart("imgFile") MultipartFile imgFile, HttpServletRequest request) {
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return logicModelService.createMaterial(createLogicMaterialDTO,imgFile,userId);
    }

    @GetMapping("/getMaterialById/{materialId}")
    @ApiOperation(value = "查询逻辑建模素材信息")
    public JsonResult getProjectById(@PathVariable String materialId) {
        return logicModelService.getMaterialById(materialId);
    }

    @PostMapping("/getMaterialList")
    @ApiOperation(value = "查询逻辑建模素材列表")
    public JsonResult getMaterialList(@RequestBody LogicMaterialPageDTO logicMaterialPageDTO) {
        return logicModelService.searchMaterialList(logicMaterialPageDTO);
    }

    @PutMapping("/updateMaterial")
    @ApiOperation(value = "更新逻辑建模素材")
    public JsonResult updateMaterial(@RequestPart("info") CreateLogicMaterialDTO createLogicMaterialDTO,
                                     @RequestPart("imgFile") MultipartFile imgFile, HttpServletRequest request) {
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return logicModelService.createMaterial(createLogicMaterialDTO,imgFile,userId);
    }

    @DeleteMapping("/deleteMaterial/{MaterialId}")
    @ApiOperation(value = "删除逻辑建模素材")
    public JsonResult deleteMaterial(@PathVariable("MaterialId") String MaterialId) {
        JsonResult jsonResult = logicModelService.deleteMaterial(MaterialId);
        return jsonResult;
    }
}
